package org.papervision3d.core.animation.track
{
	import org.papervision3d.core.animation.keyframe.AbstractKeyFrame;
	import org.papervision3d.core.log.PaperLogger;
	import org.papervision3d.core.math.Quaternion;
	import org.papervision3d.objects.DisplayObject3D;

	public class RotationTrack extends AbstractTrack
	{
		private var _q :Quaternion;
		
		public function RotationTrack(target:DisplayObject3D)
		{
			super(target);
			
			_q = new Quaternion();
		}
		
		public override function addKeyFrame(keyframe:AbstractKeyFrame):void
		{
			if(!keyframe || !keyframe.data || keyframe.data.length != 4)
			{
				PaperLogger.error("RotationTrack#addKeyFrame : keyframe data should contain 4 values (x, y, z, radians)! " + keyframe.data);
				return;
			}
			super.addKeyFrame(keyframe);
		}
		
		public override function tick(seconds:Number):void
		{
			super.tick(seconds);
			
			var x :Number, y :Number, z:Number, angle :Number;
			
			if(current)
			{
				x = current.data[0];
				y = current.data[1];
				z = current.data[2];
				
				if(next)
				{
					angle = (current.data[3] + alpha * (next.data[3] - current.data[3]));
				}
				else
				{
					angle = current.data[3];
				}
				
				_q.setFromAxisAngle(x, y, z, angle);
				
				matrix.copy3x3(_q.matrix);
			}
		}
	}
}